package main

import (
	h "bhttp"
	"binterfaces/ihttp"
	"binterfaces/ilogger"
	"blogger"
)

type ResultObj struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data any    `json:"data"`
}

// UserCreate 用户创建
type UserCreate struct {
	Name  string `json:"name" validate:"required,len=5"`
	Age   int    `json:"age" validate:"required,min=6,max=20,msg=年龄最小6，最大20"`
	Email string `json:"email" validate:"required,email"`
}

func main() {
	log := blogger.DefaultLogger(
		blogger.WithPrefix("Boot"),
		blogger.WithFormatter(ilogger.LogFormatText),
		blogger.WithBasePath("/Users/yuelong/Working/Golang/fast-boot/logs"),
	)
	engine := h.NewHttp(log, 8181)
	engine.Routes("api").
		Get(func(ctx ihttp.IHttpContext) {}).
		Post(func(ctx ihttp.IHttpContext) {}).
		Push(
			h.NewRouter("user").
				Get(func(ctx ihttp.IHttpContext) {}).
				Post(func(ctx ihttp.IHttpContext) {
					var param UserCreate
					if err := ctx.Body().PostJson(&param); err != nil {
						ctx.Log().ErrorF("创建用户信息表单解析异常: %s", err.Error())
						ctx.Response().Json(500, ResultObj{
							Code: 100500,
							Msg:  err.Error(),
							Data: "构建失败",
						})
						return
					}
					ctx.Response().Json(200, ResultObj{
						Code: 200,
						Msg:  "创建成功",
						Data: param,
					})
				}),
			h.NewRouter("home").
				Get(func(ctx ihttp.IHttpContext) {}).
				Post(func(ctx ihttp.IHttpContext) {}),
		)
	// 静态资源
	//engine.StaticEmbed()
	engine.Start()
}
